package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.ui.base.BaseActivity;
import cn.yikaojiuguo.student.ui.usercenter.ChooseProvinceActivity;

/**
 * Created by cfan_yang on 2015/12/14.
 */
public class ChooseGradeAdapter extends SimpleBaseAdapter<String>{

    //选中的标记位
    private int mChoose = -1;

    public ChooseGradeAdapter(Context context, List list) {
        super(context, list);
    }

    public ChooseGradeAdapter(Context context, List list, int choose) {
        this(context,list);
        this.mChoose = choose;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_choose_major,null);
            holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_item.setText(mList.get(position).toString());
        //恢复默认样式
        convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable
                .shape_item_choose_major));
        holder.tv_item.setTextColor(mContext.getResources().getColor(R.color.text_black));

        if(position == mChoose) {//如果被选中
            convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable
                    .shape_item_choose_major_selected));
            holder.tv_item.setTextColor(mContext.getResources().getColor(R.color.text_white));
        }

        holder.tv_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChoose = position;
                //index从0开始
                YiKaoApplication.putChooseGradeIndex(position);
                //GradeId从1开始
                YiKaoApplication.putGradeId((position + 1) + "");
                YiKaoApplication.putGradeName(getItem(position));
                LogUtil.showInfoLog("gradeId","gradeId = " + YiKaoApplication.getGradeId());
                notifyDataSetChanged();
                mContext.startActivity(ChooseProvinceActivity.newIntent());
                if(mContext instanceof BaseActivity) {
                    ((BaseActivity) mContext).finish();
                    ((BaseActivity) mContext).rightEnter();
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView tv_item;
    }
}
